package com.fitness.fitness.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.fitness.convertor.CourseCommentConvertor;
import com.fitness.fitness.mapper.CourseCommentMapper;
import com.fitness.fitness.mapper.CourseMapper;
import com.fitness.fitness.mapper.MemberMapper;
import com.fitness.fitness.modal.course.dto.CourseCommentDto;
import com.fitness.fitness.modal.course.entity.Course;
import com.fitness.fitness.modal.course.entity.CourseComment;
import com.fitness.fitness.modal.course.params.CourseCommentQueryParams;
import com.fitness.fitness.modal.course.vo.CourseCommentVO;
import com.fitness.fitness.modal.member.entity.Member;
import com.fitness.fitness.service.ICourseCommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CourseCommentServiceImpl extends ServiceImpl<CourseCommentMapper, CourseComment> implements ICourseCommentService {

    private final CourseCommentConvertor courseCommentConvertor;
    private final MemberMapper memberMapper;
    private final CourseMapper courseMapper;

    @Override
    public Page<CourseCommentVO> getCourseCommentPage(CourseCommentQueryParams queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<CourseComment> page = new Page<>(pageNum, pageSize);

        // 查询数据
        Page<CourseComment> pageVo = this.baseMapper.getAllCourseCommentPage(page, queryParams);
        Page<CourseCommentVO> retPage = courseCommentConvertor.entity2Vo(pageVo);
        if(pageVo.getTotal() > 0){
            List<Long> tmp = pageVo.getRecords().stream().map(CourseComment::getMemberId).collect(Collectors.toList());
            List<Member> members = memberMapper.getMapById(tmp);
            Map<Long,Member> memberMap = new HashMap<>();
            members.forEach(member -> memberMap.put(member.getId(), member));
            retPage.getRecords().forEach((vo)->{
                Member member = memberMap.get(vo.getMemberId());
                if(member != null){
                    vo.setAvatar(StrUtil.isBlank(member.getAvatar()) ? null : member.getAvatar());
                }
            });
            return retPage;
        }
        return retPage;
    }

    @Override
    public boolean addCourseComment(CourseCommentDto commentDto) {
        Long courseId = commentDto.getCourseId();
        Course course = courseMapper.selectById(courseId);
        commentDto.setCourseName(course.getName());
        CourseComment courseComment = courseCommentConvertor.dto2Entity(commentDto);
        return this.baseMapper.insert(courseComment) > 0;
    }

    @Override
    public boolean updateCourseComment(CourseCommentDto commentDto) {
        return false;
    }
}
